package org.cnam.mobileShopping.bean;

import java.text.ParseException;
import java.util.Date;

import org.cnam.mobileShopping.util.PatternUtil;
import org.cnam.mobileShopping.wsclient.WSEntityListId;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * Représentation d'un liste au sein de notre application.
 * 
 * Cette dernière va avoir une référence vers son créateur
 * 
 * @author lolo
 * 
 */
@WSEntityListId("mList")
public class MList {
	private Integer mlistid;

	private String label;

	private Date createdatetime;

	private Short status;

	private String commonid;

	private int version;

	private boolean issynchr;

	public Integer getMlistid() {
		return mlistid;
	}

	public void setMlistid(Integer mlistid) {
		this.mlistid = mlistid;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public Date getCreatedatetime() {
		return createdatetime;
	}

	public void setCreatedatetime(Date createdatetime) {
		this.createdatetime = createdatetime;
	}

	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public MList() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MList(Integer mlistid, String label, Date createdatetime,
			Integer status, int version, String commonid) {
		super();
		this.mlistid = mlistid;
		this.label = label;
		this.status = status.shortValue();
		this.createdatetime = createdatetime;
		this.version = version;
		this.commonid = commonid;
	}

	public static MList parseJSON(JSONObject jsonObject) throws JSONException {
		Date d;
		try {
			d = Constants.formatDateEN.parse(jsonObject
					.getString("createdatetime"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			Log.e("MList", "error on parse createddatetime");
			d = new Date();
		}

		return new MList(jsonObject.getInt("mlistid"),
				jsonObject.getString("label"), d, jsonObject.getInt("status"),
				jsonObject.getInt("version"), jsonObject.getString("commonid"));
	}

	// @Override
	public String toString() {

		// just pr debeug, a supprimer par la suite
		StringBuffer s = new StringBuffer();
		s.append(this.label);
		if (status == Constants.statusList.ENCOURSCOURSE.getId()) {
			s.append("(verrouillé)");
		} else {
			s.append("(ouverte)");
		}
		s.append(" - Le " + PatternUtil.formatDate(this.createdatetime));

		return s.toString();
	}

	public String getCommonid() {
		return commonid;
	}

	public void setCommonid(String commonid) {
		this.commonid = commonid;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public boolean isSynchr() {
		return issynchr;
	}

	public void setSynchr(boolean isSynchr) {
		this.issynchr = isSynchr;
	}

}
